package com.hh.store.service.impl;

import com.hh.store.VO.*;
import com.hh.store.mapper.StoreMapper;
import com.hh.store.service.StoreService;
import com.hh.utils.PageVo;
import com.hh.utils.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author Hanh
 * @date 2023/8/16 15:59s
 */
@Service
public class StoreServiceImpl implements StoreService {

    @Resource
    private StoreMapper storeMapper;



    /**
     * 分页查询
     * @param queryStorePageVo
     * @return
     */
    @Override
    public ResultVo queryStorePage(QueryStorePageVo queryStorePageVo) {
        //计算总条数
        //1.计算总的数据量
        Integer totalNumber = storeMapper.getAllStoreCount(queryStorePageVo);
        //2.计算偏移量
        queryStorePageVo.setOffsize((queryStorePageVo.getPageIndex()-1)* queryStorePageVo.getPageSize());
        //3.获取合适条件的数据
        List<StoreForQueryWebVo> storeList = storeMapper.getStoreByQuery(queryStorePageVo);
        //4.计算总页数
        PageVo<StoreForQueryWebVo> pageVo = new PageVo<>();
        Integer totalPage =totalNumber%queryStorePageVo.getPageSize()==0?totalNumber/queryStorePageVo.getPageSize():
                totalNumber/queryStorePageVo.getPageSize()+1;
        pageVo.setTotalNumber(totalNumber);
        pageVo.setPageIndex(queryStorePageVo.getPageIndex());
        pageVo.setDataList(storeList);
        pageVo.setOffSize((queryStorePageVo.getPageIndex()- 1)* queryStorePageVo.getPageSize());
        pageVo.setTotalPage(totalPage);
        return ResultVo.success(pageVo);

    }

    /**
     * 根据库区code拿到其对应的所属库位信息
     * @param areaCode
     * @return
     */
    @Override
    public ResultVo getStoreByAreaCode(String areaCode) {
        List<CommonGetStoreVo> storeVos = storeMapper.getStoreByAreaCode(areaCode);
        return ResultVo.success(storeVos);
    }

    /**
     * 修改状态,启用禁用
     * @param statusVo
     * @return
     */
    @Override
    public ResultVo updateStatus(UpdateStatusVo statusVo) {
        storeMapper.updateStatus(statusVo);
        return ResultVo.success("状态修改成功！");
    }

    /**
     * 根据法人拿到库房信息
     * @param legalpersonCode
     * @return
     */
    @Override
    public ResultVo getStoreRoomByLegalPersonCode(String legalpersonCode) {
        List<StoreRoomVo> storeRoomVos = storeMapper.getStoreRoomByLegalPersonCode(legalpersonCode);
        return ResultVo.success(storeRoomVos);
    }

    /**
     * 根据库房code拿到库区的信息
     * @param storeRoomCode
     * @return
     */
    @Override
    public ResultVo getStoreAreaByRoomCode(String storeRoomCode) {
        List<StoreAreaVo> storeAreaVos = storeMapper.getStoreAreaByRoomCode(storeRoomCode);
        return ResultVo.success(storeAreaVos);
    }

    /**
     * 库位添加
     * @param addStoreVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo addStore(AddStoreVo addStoreVo) {

        //用于记录第几个code,便于消息的反馈
//        Integer flag = 1;
        try {
            //拿到原有数据中具备法人信息的库位编码code及法人code
            List<LegalStoreVo> legalStoreVoList = storeMapper.getLegalByCode(addStoreVo.getStore());
            //1.根据areaCode删除其库位信息
            storeMapper.removeStoreByArea(addStoreVo.getAreaCode());
           /* //2，进行批量校验查重
            //2.1创建临时表
            storeMapper.createTempTable();
            //2.2将等待验证的code与name存储在临时表中
            storeMapper.insertTempTable(addStoreVo.getStore());
            //2.3进行批量查重校验
            List<Integer> checkRepeat = storeMapper.checkRepeat();
            for (int i = 0; i < addStoreVo.getStore().size(); i++) {
                Integer result = checkRepeat.get(i);
                if (result > 0) {
                    // 存在重复的name或code
                    return ResultVo.error("您的第"+i+"个库区录入的编码或名称重复，请重新录入");
                }
            }*/
            //对于name进行批量查重
            Integer existName = storeMapper.getExistStoreName(addStoreVo.getStore());
            if (existName >0){
                return ResultVo.error("录入的库位名称出现重复，请重新录入！");
            }
            //对于code进行批量查重
            Integer existCode = storeMapper.getExistStoreCode(addStoreVo.getStore());
            if (existCode>0){
                return ResultVo.error("录入的库位名称出现重复，请重新录入！");
            }
            //3.保留原有的法人信息
            if (legalStoreVoList.size()!=0){
                for (int i = 0; i < addStoreVo.getStore().size(); i++) {
                    for (LegalStoreVo store : legalStoreVoList){
                        if (store.getCode().equals(addStoreVo.getStore().get(i).getCode())){
                            addStoreVo.getStore().get(i).setLegalCode(store.getLegalCode());
                        }
                    }
                }
            }

        //4.执行插入
            storeMapper.insertStoreList(addStoreVo);
            /*for(AddStoreCodeNameVo storeCodeNameVo:addStoreVo.getStore()){
                //3.校验查重
                //存放已经存在录入code的库位数量
                Integer countExistStoreCode = storeMapper.getStoreByCode(storeCodeNameVo.getCode());
                //存放已经存在录入name的库位数量
                Integer countExistStoreName = storeMapper.getStoreByName(storeCodeNameVo.getName());
                if (countExistStoreCode>0){
                }else if (countExistStoreName>0){
                    throw new Exception();
                }
                storeMapper.insertStore(addStoreVo.getAreaCode(),storeCodeNameVo.getName(),storeCodeNameVo.getCode());
                flag++;
            }*/
            return ResultVo.success("插入成功！");
        }catch (Exception e){
            e.printStackTrace();
            //手工回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultVo.error("插入失败");
        }


    }

    /**
     * 库位修改
     * @param updateStoreVo
     * @return
     */
    @Override
    public ResultVo updateStore(UpdateStoreVo updateStoreVo) {
        //存放已经存在录入name的库位数量
        Integer countExistStoreName = storeMapper.getStoreByName(updateStoreVo.getName());
        if (countExistStoreName>0){
            return ResultVo.error("库位的名称已存在，请重新录入正确名称!");
        }
        //执行修改语句
        storeMapper.updateStoreName(updateStoreVo);
        return ResultVo.success("修改完成！");
    }

    @Override
    public ResultVo removeStore(String code) {
        if (code==null){
            return ResultVo.error("您做了一次无用操作");
        }
        storeMapper.removeStore(code);
        return ResultVo.success("删除成功");
    }

}
